// packageUser/pages/message/message.js
import MessageService from '../../../service/MessageService';
import moment from '../../../libs/moment.min.js';
import { throttle } from '../../../utils/util.js';

const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    buttons:[{
      type: 1,
      label: '救兵平台消息'
    }, {
      type: 2,
      label: '投诉我的'
    }, {
      type: 3,
      label: '我投诉的'
    }],
    type: 1,
    compRes: {list:[], page:{}},
    msgRes:{list:[], page: {}},
    param: {
      pageNum: 1,
      pageSize: 30,
      deviceType: 'wx',
      receiveUser: app.globalData.user.id,
      receiveUserType: app.globalData.user.userType
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.messageService = new MessageService();
    const {param} = this.data;
    this.loadSysMsg(param);
  },
  onTypeChange: function(e){
    this.setData({type: e.detail.type});
    if (e.detail.type == 1){
      const { param } = this.data;
      param.pageNum = 1;
      this.setData({ param, msgRes: { list: [], page: {} } });
      this.loadSysMsg(param);
    }else{
      this.loadComplains(e.detail.type);
    }
    
  },
  loadSysMsg(param){
    const _ = this;
    const { msgRes } = this.data;
    this.messageService.listForPage(param).then(res=>{
      if(res.status == 0){
        msgRes.list = [...msgRes.list, ...res.data.list];
        msgRes.page = res.data.page;
        _.setData({ msgRes });
      }
    })
  },
  loadComplains(type){
    const _ = this;
    this.messageService.complain({type}).then(function (res) {
      console.log(res);
      if(res.status == 0){
        const list = res.data.list.map(c=>{
          const images = c.fileList && c.fileList.map(f=>{
            return f.fileUrl;
          })
          c.fileList = images;
          c.createTime = moment(c.createTime).format('YYYY-MM-DD');
          return c;
        })
        res.data.list = list;
        _.setData({compRes: res.data});
      }
    });
  },
  onItemTap:function(e){
    console.log(e.target.dataset.id)
    const {id, type} = e.target.dataset;
    wx.navigateTo({
      url: '/packageUser/pages/message/complain-detail?id=' + id + "&type=" + type
    })
  },
  onScollBottom: function(e){
    console.log(e);
    const _ = this;
    const { msgRes, param } = this.data;
    throttle(function(){
      if (msgRes.page.currentNo < msgRes.page.totalPage){
        param.pageNum++;
        _.setData({ param })
        _.loadSysMsg(param);
      }
    }, 1000)()
  }
})